import axios from 'axios';
import { Message } from 'element-ui';
const http = axios.create({
  // 设置超时时间
  timeout: 10000,
  baseURL: '/api'
  // 其他axios的配置详见：https://www.kancloud.cn/yunye/axios/234845
});

// 添加请求拦截器
http.interceptors.request.use(
  config => {
    // 在请求发送前做些什么
    // 例如添加自定义请求头:
    // if(localStorage.token) config.headers['token'] = localStorage.token;
    return config;
  },
  error => {
    console.log(error);
    return Promise.reject(error);
  }
);

http.interceptors.response.use(
  response => {
    const res = response.data
    if (res.code != 20000) {
      Message({
        message: res.message || res,
        type: 'error',
      })
      return Promise.reject(new Error(res.message || res))
    }
    else {
      return res.data;
    }

  },
  error => {
    return Promise.reject(error);
  }
);

export default http;
